package com.yeren.czbk.oneToMany;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

import com.yeren.czbk.oneToMany.entity.Department;
import com.yeren.czbk.oneToMany.entity.Employee;



@SuppressWarnings("deprecation")
public class App {
	private static SessionFactory ssessionFactory;
	//初始化ssessionFactory
	static{
		Configuration cfg=new Configuration();
		cfg.configure("hibernate.cfg.xml");//读取配置文件
		ssessionFactory=cfg.buildSessionFactory();//根据配置生成session工厂
	}
//	@Test
	public void testSave() throws Exception{
		Session session=ssessionFactory.openSession();//打开一个新的事务
		Transaction t =session.beginTransaction();//开始事务
		//-------------------------------------------------
		//新建对象
		Department department=new Department();
		department.setName("研发部");
		Employee employee1=new Employee();
		employee1.setName("张三");
		Employee employee2=new Employee();
		employee2.setName("李四");
		//关联起来
		employee1.setDepartment(department);
		employee2.setDepartment(department);
		department.getEmployees().add(employee1);
		department.getEmployees().add(employee2);
		//保存（被依赖的放在前面保存）
		session.save(department);
		session.save(employee1);
		session.save(employee2);
		
		
		
		//-------------------------------------------------
		session.getTransaction().commit();
		session.close();//关闭session，释放资源
	}
	
	//获取，可以获取到关联的对方
//	@Test
	public void testGet() throws Exception{
		Session session=ssessionFactory.openSession();//打开一个新的事务
		Transaction t =session.beginTransaction();//开始事务
		//-------------------------------------------------
		
		//获取一方
		Department department=(Department) session.get(Department.class, 1);
		System.out.println(department);
		System.out.println(department.getEmployees());
		//获取另一方信息
		Employee employee=(Employee) session.get(Employee.class, 1);
		System.out.println(employee);
		System.out.println(employee.getDepartment());
		
		//-------------------------------------------------
		session.getTransaction().commit();
		session.close();//关闭session，释放资源
	}
	
	//解除关联关系
//		@Test
		public void testRemoveRelation()throws Exception{
			Session session=ssessionFactory.openSession();//打开一个新的事务
			Transaction t =session.beginTransaction();//开始事务
			//-------------------------------------------------
			//从员工方解除
			Employee employee=(Employee) session.get(Employee.class, 1);
			employee.setDepartment(null);
			//从部门方解除(与inverse有关系，为false可以解除)
			Department department=(Department) session.get(Department.class,1);
			department.getEmployees().clear();
			
			//-------------------------------------------------
			session.getTransaction().commit();
			session.close();//关闭session，释放资源
		}
	
	
	//删除对象，对关联对象的影响
	@Test
	public void testDelete()throws Exception{
		Session session=ssessionFactory.openSession();//打开一个新的事务
		Transaction t =session.beginTransaction();//开始事务
		//-------------------------------------------------
		//删除员工方，（多方）,对方没有影响
		Employee employee=(Employee) session.get(Employee.class, 1);
		session.delete(employee);
		//删除部门方（一方）
		//a，如果没有关联的员工：能删除
		//b,如果有关联的员工且inverse=true,由于不能维护关联关系，所有会直接执行删除，就会有异常
		//c,如果有关联的员工且inverse=false,由于可以维护关联关系，他就会先把关联的员工的外键设置为空，然后删除自己
		Department department=(Department) session.get(Department.class, 6);
		session.delete(department);
		
		//-------------------------------------------------
		session.getTransaction().commit();
		session.close();//关闭session，释放资源
	}
	
}
